/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.models.beans;

import com.models.entities.TblUser;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class UserSessionBean {

    @PersistenceContext(unitName = "cungonbaiPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    /**
     * check login
     *
     * @param username
     * @param password
     * @return
     */
    public TblUser checkLogin(String username, String password) {
        Query query = em.createNamedQuery("TblUser.findByUsername");
        query.setParameter("username", username);
        try {
            TblUser user = (TblUser) query.getSingleResult();
            if (user != null && user.getPassword().equals(password)) {
                return user;
            }
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return null;
    }

    //////ket thuc
    /**
     * insert user
     *
     * @param user
     * @return
     */
    public boolean register(TblUser user) {
        try {
            persist(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //////ket thuc
    /**
     * kiem tra su ton tai cua user
     *
     * @param username
     * @return
     */
    public boolean isExistsUsername(String username) {
        Query query = em.createNamedQuery("TblUser.findByUsername");
        query.setParameter("username", username);
        System.out.println(username);
        try {
            TblUser user = (TblUser) query.getSingleResult();
            System.out.println(user.getUsername());
            if (user != null) {
                return true;
            }
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return false;
    }

    ///// ket thuc
    /**
     * update profile cua user
     *
     * @param user
     * @return
     */
    public boolean updateProfile(TblUser user) {

        try {
            TblUser u = em.find(TblUser.class, user.getUserId());
            if (u != null) {
                u.setEmail(user.getEmail());
                u.setFullName(user.getFullName());
                u.setRating(user.getRating());
                u.setRole(user.getRole());
                u.setStatus(user.getStatus());
                u.setUserLevel(user.getUserLevel());

                persist(u);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //// ket thuc
    /**
     * update password
     *
     * @param userID
     * @param password
     * @return
     */
    public boolean updatePassword(int userID, String password) {
        TblUser user = em.find(TblUser.class, userID);
        if (user != null) {
            user.setPassword(password);
            return true;
        }
        return false;
    }
    ////ket thuc
    
    /**
     * kiem tra su ton tai cua email
     */
    public boolean isExistsEmail(String email) {
        Query query = em.createNamedQuery("TblUser.findByEmail");
        query.setParameter("email", email);
        try {
            TblUser user = (TblUser) query.getSingleResult();
            System.out.println(user.getUsername());
            if (user != null) {
                return true;
            }
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
